package controller;

import service.ForgetPwd;
import common.Message;
import common.MessageType;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import util.EmailCheck;
import util.MD5;
import util.StartCountdownTimer;

import java.io.ObjectOutputStream;
import java.net.Socket;

public class Forget {
    @FXML
    private TextField email;
    @FXML
    private TextField pwd;
    @FXML
    private TextField sure;
    @FXML
    private TextField code;
    @FXML
    private Label error;
    private long lastClickTime;
    @FXML
    private Button getCodeButton;
    @FXML
    public void getCode() {
        if (email.getText().equals("")) {
            error.setText("账号为空");
        } else if (!EmailCheck.checkEmail(email.getText())) {
            error.setText("账号格式错误");
        } else {
            getCodeButton.setOnMouseClicked(event -> {
                if (System.currentTimeMillis() - lastClickTime > 60000) { // 按钮上一次被点击的时间距离当前时间已经超过 60 秒
                    // 执行发送验证码操作
                    error.setText("格式正确");
                    Socket socket = null;
                    try {
                        socket = new Socket("127.0.0.1", 10086);
                        Message ms = new Message();
                        ms.setState(MessageType.Email_Send);
                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                        oos.writeObject(email.getText());
                    } catch (Exception e) {
                        System.out.println("获取邮箱出错");
                        e.printStackTrace();
                    } finally {
                        try {
                            if (socket != null) {
                                socket.close();
                            }
                        } catch (Exception e) {
                            System.out.println("关闭socket出错");
                            e.printStackTrace();
                        }
                    }
                    // 更新时间戳，禁用按钮并开始倒计时计时器
                    lastClickTime = System.currentTimeMillis();
                    getCodeButton.setDisable(true);
                    StartCountdownTimer.startCountdownTimer(getCodeButton,60);//倒计时
                }
            });
        }
    }
    @FXML
    private Label pwderror;
    @FXML
    private Label error1;
    @FXML
    public void Ok(){
        if(pwd.getText().equals("")||sure.getText().equals("")){
            pwderror.setText("密码为空");
            return;
        } else if(!EmailCheck.checkEmail(email.getText())){
            error1.setText("账号格式错误");
            return;
        } else if (!pwd.getText().equals(sure.getText())) {
            pwderror.setText("两次密码不一致");
            return;
        }
        error.setText("账号格式正确");
        pwderror.setText("两次密码一致");
        if(ForgetPwd.ForgetPwd(email.getText(), MD5.getPWD(pwd.getText()),code.getText())){
            Stage stage=(Stage) cancel.getScene().getWindow();
            stage.close();
        }else{
                error.setText("验证码错误或账号不存在");
        }
    }
    @FXML
    private Button cancel;
    @FXML
    public void Cancel(){
        Stage stage=(Stage) cancel.getScene().getWindow();
        stage.close();
    }
    @FXML
    private Label title;
    public Label getTitle() {
        return title;
    }
}
